#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
过滤器严格性测试主运行脚本
一键执行完整的测试流程：数据生成 -> 测试执行 -> 结果分析

作者: AI Assistant
创建时间: 2025-01-14

使用方法:
    python run_filter_test.py
"""

import sys
import os
from datetime import datetime

# 添加项目根目录到路径
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))

try:
    from test_data_generator import FilterStrictTestDataGenerator
    from test_executor import FilterStrictTestExecutor
    from test_analyzer import FilterStrictTestAnalyzer
except ImportError as e:
    print(f"导入错误: {e}")
    print("请确保所有测试文件都在正确的位置")
    sys.exit(1)

def print_header(title: str):
    """
    打印标题头
    """
    print("\n" + "=" * 80)
    print(f" {title} ")
    print("=" * 80)

def print_section(title: str):
    """
    打印章节标题
    """
    print("\n" + "-" * 60)
    print(f" {title} ")
    print("-" * 60)

def main():
    """
    主测试流程
    """
    start_time = datetime.now()
    
    print_header("过滤器严格性测试 - 完整测试流程")
    print(f"开始时间: {start_time.strftime('%Y-%m-%d %H:%M:%S')}")
    print("\n测试目标: 验证放松状态被误判为兴奋状态的问题")
    print("测试范围: 边缘数据、极端数据、连续变化数据")
    
    try:
        # 第一步：数据生成验证
        print_section("第一步: 测试数据生成验证")
        print("正在验证测试数据生成器...")
        
        generator = FilterStrictTestDataGenerator()
        
        # 生成少量示例数据进行验证
        sample_data = generator.generate_normal_relaxed(3)
        print(f"✓ 成功生成 {len(sample_data)} 个正常放松状态样本")
        
        boundary_data = generator.generate_boundary_relaxed_data(3)
        print(f"✓ 成功生成 {len(boundary_data)} 个边界放松状态样本")
        
        print("\n示例数据预览:")
        print("正常放松状态样本:")
        for i, data in enumerate(sample_data[:2]):
            d = data['data']
            print(f"  样本{i+1}: Low_Alpha={d['low_alpha']:.3f}, High_Alpha={d['high_alpha']:.3f}, Low_Beta={d['low_beta']:.3f}, High_Beta={d['high_beta']:.3f}")
        
        print("边界放松状态样本:")
        for i, data in enumerate(boundary_data[:2]):
            print(f"  样本{i+1}: Low_Alpha={data['low_alpha']:.3f}, High_Alpha={data['high_alpha']:.3f}, Low_Beta={data['low_beta']:.3f}, High_Beta={data['high_beta']:.3f}")
        
        # 第二步：执行完整测试
        print_section("第二步: 执行完整分类测试")
        print("正在执行所有测试数据集的分类测试...")
        print("这可能需要几分钟时间，请耐心等待...")
        
        executor = FilterStrictTestExecutor()
        results = executor.run_all_tests()
        
        # 显示测试进度和结果
        overall_stats = results['overall_stats']
        print(f"\n✓ 测试完成!")
        print(f"  - 总数据集数: {overall_stats['total_datasets']}")
        print(f"  - 总样本数: {overall_stats['total_samples']}")
        print(f"  - 总体准确率: {overall_stats['overall_accuracy']:.2%}")
        
        # 显示关键数据集结果
        print("\n关键数据集结果:")
        key_datasets = {
            'normal_relaxed': '正常放松',
            'boundary_relaxed': '边界放松', 
            'beta_exceed_relaxed': 'Beta超出放松',
            'mild_excited': '轻微兴奋',
            'boundary_excited': '边界兴奋'
        }
        
        for dataset_key, dataset_name in key_datasets.items():
            accuracy = overall_stats['dataset_accuracies'].get(dataset_key, 0)
            status = "✓" if accuracy >= 0.8 else "⚠" if accuracy >= 0.6 else "✗"
            print(f"  {status} {dataset_name}: {accuracy:.1%}")
        
        # 第三步：结果分析
        print_section("第三步: 深度结果分析")
        print("正在生成详细分析报告...")
        
        # 保存测试结果
        executor.save_results_to_file()
        
        # 执行深度分析
        analyzer = FilterStrictTestAnalyzer()
        
        if analyzer.results_data:
            # 生成综合报告
            report_file = analyzer.save_report()
            print(f"✓ 详细分析报告已生成: {report_file}")
            
            # 显示关键发现
            print("\n🔍 关键发现:")
            
            # 分析准确率问题
            if overall_stats['overall_accuracy'] < 0.7:
                print("  ⚠ 总体准确率偏低，存在系统性分类问题")
            
            # 分析具体问题
            problem_datasets = [
                (k, v) for k, v in overall_stats['dataset_accuracies'].items() 
                if v < 0.6
            ]
            
            if problem_datasets:
                print("  ⚠ 发现严重分类问题的数据集:")
                for dataset, accuracy in problem_datasets:
                    print(f"    - {dataset}: {accuracy:.1%}")
            
            # 显示优化建议
            suggestions = results.get('optimization_suggestions', [])
            if suggestions:
                print("\n💡 主要优化建议:")
                for i, suggestion in enumerate(suggestions[:5], 1):
                    print(f"  {i}. {suggestion}")
            
            # Beta波问题分析
            beta_analysis = analyzer.analyze_beta_wave_sensitivity()
            if beta_analysis.get('boundary_issues') or beta_analysis.get('threshold_problems'):
                print("\n🎯 Beta波问题确认:")
                if beta_analysis.get('boundary_issues'):
                    print("  ✗ Beta波边界处理存在问题")
                if beta_analysis.get('threshold_problems'):
                    print("  ✗ Beta波阈值设置过于严格")
                print("  → 这证实了用户反馈的'放松状态被误判为兴奋'问题")
        
        # 第四步：总结和建议
        print_section("第四步: 测试总结和行动建议")
        
        end_time = datetime.now()
        duration = end_time - start_time
        
        print(f"测试完成时间: {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"总耗时: {duration.total_seconds():.1f} 秒")
        
        # 根据结果给出行动建议
        print("\n📋 行动建议优先级:")
        
        if overall_stats['overall_accuracy'] < 0.6:
            print("  🔴 高优先级: 立即优化分类算法，准确率过低")
        elif overall_stats['overall_accuracy'] < 0.8:
            print("  🟡 中优先级: 需要优化分类算法，提升准确率")
        else:
            print("  🟢 低优先级: 分类算法基本可用，可进行细节优化")
        
        # 具体技术建议
        print("\n🔧 具体技术建议:")
        
        boundary_relaxed_acc = overall_stats['dataset_accuracies'].get('boundary_relaxed', 1)
        beta_exceed_acc = overall_stats['dataset_accuracies'].get('beta_exceed_relaxed', 1)
        
        if boundary_relaxed_acc < 0.8:
            print("  1. 调整放松状态Beta波上限: 0.25 → 0.28")
        
        if beta_exceed_acc < 0.6:
            print("  2. 实现Beta波边界平滑过渡算法")
        
        if any(acc < 0.7 for acc in overall_stats['dataset_accuracies'].values()):
            print("  3. 优化置信度计算，提高分类确定性")
        
        print("\n📁 生成的文件:")
        print(f"  - 测试结果: {os.path.join(os.path.dirname(__file__), 'test_results.json')}")
        print(f"  - 分析报告: {os.path.join(os.path.dirname(__file__), 'analysis_report.txt')}")
        
        print("\n✅ 过滤器严格性测试完成！")
        print("   请查看生成的报告文件获取详细信息。")
        
    except Exception as e:
        print(f"\n❌ 测试过程中发生错误: {e}")
        print("\n错误详情:")
        import traceback
        traceback.print_exc()
        return 1
    
    return 0

if __name__ == "__main__":
    # 检查Python版本
    if sys.version_info < (3, 6):
        print("错误: 需要Python 3.6或更高版本")
        sys.exit(1)
    
    # 检查必要的依赖
    try:
        import numpy as np
    except ImportError:
        print("错误: 需要安装numpy库")
        print("请运行: pip install numpy")
        sys.exit(1)
    
    # 运行主测试流程
    exit_code = main()
    sys.exit(exit_code)